package com.kgc.scd.service.impl;

import com.kgc.scd.bean.Sign;
import com.kgc.scd.bean.User;
import com.kgc.scd.mapper.UserMapper;
import com.kgc.scd.service.SignService;
import com.kgc.scd.service.UserService;
import com.kgc.scd.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created On : 2022/6/10.
 * <p>
 * Author     : nainai
 * <p>
 * Description: UserServiceImpl
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private SignService signService;

    @Override
    public Integer getCreditById(String userId) {
        log.info("用户：{}，开始查询积分");
        return userMapper.selectByPrimaryKey(userId).getUserCredit();
    }

    @Override
    public boolean addCredit(String userId, Integer credit) {
        log.info("用户：{}，增加积分：{}", userId, credit);

        // 执行增加积分操作
        User user = userMapper.selectByPrimaryKey(userId);
        user.setUserCredit(user.getUserCredit() + credit);

        // 返回增加是否成功
        return userMapper.updateByPrimaryKey(user) > 0;
    }

    @Override
    public boolean subCredit(String userId, Integer credit) {
        log.info("用户：{}，扣减积分：{}", userId, credit);

        // 执行积分扣减操作
        User user = userMapper.selectByPrimaryKey(userId);
        user.setUserCredit(user.getUserCredit() - credit);

        // 返回是否扣减成功
        return userMapper.updateByPrimaryKey(user) > 0;
    }

    @Override
    public Map<String, String> UserSign(String userId) {
        Map<String, String> map = new HashMap<>();

        // 当前签到时间
        Date date = new Date();

        log.info("用户：{}开始签到", userId);
        User user = userMapper.selectByPrimaryKey(userId);

        log.info("当前正在签到用户：{}", user);
        // 修改连续签到天数，如果累积签到天数不为0，说明不是第一次签到，进行连续签到判断
        if (!(user.getUserTotalSign() == null || user.getUserTotalSign() == 0)){

            // 判断是否是当天重复签到
            if (DateUtil.checkTheSameDay(date, user.getUserSigndate())){
                log.info("用户：{}，重复签到", user.getUserId());
                map.put("result", "当天请勿重复签到！");
                return map;
            }

            // 判断上次签到和当前是否为同月，并且是连续的天
            if (DateUtil.checkDateConsecutive(user.getUserSigndate(), date)){
                user.setUserContinueSign(user.getUserContinueSign() + 1);
            } else {
                // 如果不为连续的天，清空连续天数
                user.setUserContinueSign(1);
            }
        } else {
            // 为第一次签到
            user.setUserContinueSign(1);
        }

        // 更新签到时间、累积签到天数、连续签到天数，每次签到积分+1
        user.setUserTotalSign(user.getUserTotalSign() + 1);
        user.setUserSigndate(date);
        user.setUserCredit(user.getUserCredit() + 1);

        // 判断是否连续签到了7天，连续签到七天，增加5积分
        if (user.getUserContinueSign() % 7 == 0) {
            user.setUserCredit(user.getUserCredit() + 5);
        }

        log.info("用户签到结束，更新结果为：{}", user);

        // 更新签到流水表
        LocalDate localDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDate();
        Sign sign = Sign.builder()
                .userId(user.getUserId())
                .signYear(localDate.getYear())
                .signMonth(localDate.getMonthValue())
                .signDay(localDate.getDayOfMonth())
                .signdate(user.getUserSigndate())
                .build();
        log.info("流水表更新对象：{}", sign);
        signService.addSign(sign);

        if (userMapper.updateByPrimaryKey(user) > 0) {
            map.put("result", "true");
            return map;
        }
        map.put("result", "系统异常！");
        return map;

    }

    @Override
    public boolean addUserCredit(String userId, Integer credit) {
        log.info("用户：{}，增加积分：{}", userId, credit);
        // 用户详情
        User u = userMapper.selectByPrimaryKey(userId);

        User user = User.builder()
                .userId(userId)
                .userCredit(u.getUserCredit() + credit)
                .build();

        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public User queryUserById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }


}
